package eabnp.eflow.api.impl;

import java.util.Iterator;

import eabnp.basic.result.SGResult;
import eabnp.eflow.api.IAppInterface4EFlow;
import eabnp.eflow.api.TodoItemInfo;
import eabnp.eflow.info.ctrlinfo.processctrl.EditorAuthCtrl;
import eabnp.eflow.info.ctrlinfo.processctrl.actresult.ActResultListCtrl;
import eabnp.eflow.info.ctrlinfo.processctrl.actresult.ProcessChangeCtrl;
import eabnp.eflow.misc.formula.FormulaArray;


public class AppInterface4EFlowImpl implements IAppInterface4EFlow {
	
 
public AppInterface4EFlowImpl() {
	
}

	@Override
	public SGResult getTodoItemInfo(String userUN, String flowUN, String processId, String appData) {
		String fileId = getValueByKey(appData, "id");
		String fileType = getValueByKey(appData, "fileType");
		TodoItemInfo todo = new TodoItemInfo();
		AppRmiAgent agt = new AppRmiAgent();
		todo = agt.setTodo(todo,fileType,fileId);
		SGResult sr = SGResult.newSuccessResult(todo);
		return  sr;
	}

	@Override
	public SGResult operateActResult(String userUN, String flowUN, String processId, ActResultListCtrl act, String appData) {
		
		ProcessChangeCtrl pcc = act.getProcessChangeCtrl();
		AppRmiAgent agt = new AppRmiAgent();
		if (pcc.isRemoveFromEditor()) {
			
		}
		Iterator editorIt = pcc.getEditorAuthCtrlIterator();
	      if (editorIt != null) {
	        Iterator userIt = null;
	        while (editorIt.hasNext()) {
	          EditorAuthCtrl eai = (EditorAuthCtrl)editorIt.next();
	          userIt = eai.getUserRangeListIterator();
	          agt.setAcc(act);
	          //setAccessUsers(userIt, fileId, true, userUN, pcc.getStepName());
	        }
	      }
	      SGResult sr = SGResult.newSuccessResult(act.toSimpleActResultCtrl());
		return sr;
	}

	@Override
	public SGResult validateFormula(String arg0, String arg1, String arg2, FormulaArray arg3, String arg4) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	private String getValueByKey(String data, String key) {
		if ((data == null) || (key == null)) {
			return null;
		}

		String[] dics = data.split(";");
		if ((dics != null) && (dics.length > 0)) {
			String[] dicDesc = (String[]) null;
			String[] arrayOfString1 = dics;
			int i = 0;
			for (int j = arrayOfString1.length; i < j; ++i) {
				String temp = arrayOfString1[i];
				dicDesc = temp.split("=");
				if ((dicDesc != null) && (dicDesc.length == 2) && (key.equals(dicDesc[0]))) {
					return dicDesc[1];
				}
			}
		}

		return null;
	}
	
	public static AppInterface4EFlowImpl getInstance(){
		return new AppInterface4EFlowImpl();
	}

}
